/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package spriteeditor;

import spriteeditor.sprite.Sprite;
import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author sergey.ivchenko
 */
public class ImagePane extends javax.swing.JPanel
    implements Runnable
{

    private int m_camX;
    private int m_camY;
    private int delay = 100;
    private double m_zoom = 1.0;
    public static final Color bgColor = new Color(204, 204, 204);
    public ImagePane ()
    {
        super();
        new Thread(this).start();
    }
    public double getZoom()
    {
        return m_zoom;
    }

    public void changeZoom (double d_zoom)
    {
        m_zoom += d_zoom;
        if(m_zoom < 0.2)
            m_zoom = 0.2;
        else if(m_zoom > 5.0)
            m_zoom = 5.0;
        repaint();
    }

    public void setZoom(double m_zoom)
    {
        this.m_zoom = m_zoom;
    }

    public void moveCam(int dx, int dy)
    {
        m_camX += dx;
        m_camY += dy;
        repaint();
    }

    public void resetCam()
    {
        m_camX = 0;
        m_camY = 0;
        repaint();
    }

    public void centerCam()
    {
        m_camX = getWidth() / 2;
        m_camY = getHeight() / 2;
        repaint();
    }

    public int getCamX()
    {
        return m_camX;
    }

    public void setCamX(int m_camX)
    {
        this.m_camX = m_camX;
    }

    public int getCamY()
    {
        return m_camY;
    }

    public void setCamY(int m_camY)
    {
        this.m_camY = m_camY;
    }

     public void run()
    {
        while(true)
        {
            repaint();
            try
            {
                Thread.sleep(delay);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        g.setColor(bgColor);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(Color.GRAY);
        g.drawLine(getCamX(), 0, getCamX(), getHeight());
        g.drawLine(0, getCamY(), getWidth(), getCamY());


        Sprite s = SpriteEditorView.getSprite();
        if(s == null)
            return;
        if(s.getCurFrame() != null)
        {
            s.getCurFrame().paintFrame(g, getCamX(), getCamY());
        }
        else if (s.getCurAnim() != null)
        {
            s.getCurAnim().paintAnimFrame(g, getCamX(), getCamY());
            s.getCurAnim().update();
        }
        else
        {
            s.paintSpriteImage(g, getCamX(), getCamY(), getZoom());
        }
        g.setColor(Color.GRAY);
        g.drawString("Zoom: " + (int)(m_zoom * 100) + "%", 10, 15);
    }
}
